package cn.com.qingz.core.utils;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @ClassName: JSONUtils
 * @Description: TODO(通用json操作类)
 * @author mb.wang  
 * @date 2013-10-18 下午4:58:15
 * 
 */
public final class JSONUtils {

	/**
	 * @Title: objectToJson
	 * @Description: TODO(将任意一个对象转化为json字符串)
	 * @author mb.wang  
	 * @date 2013-10-18 下午5:00:18
	 * @param t
	 * @return String  返回类型
	 * @throws JsonProcessingException 
	 */
	public static <T> String objectToJson(T t) throws JsonProcessingException{
		final ObjectMapper om = new ObjectMapper();
		
		return om.writeValueAsString(t);
	}
	
	public static <T> T jsonToEntity(String json,Class<T> t) throws JsonParseException, JsonMappingException, IOException{
		if(StringUtils.isBlank(json)||json.length()<10){
			return null;
		}
		
		final ObjectMapper om = new ObjectMapper();
		om.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);

		return ((T) om.readValue(json, t));
	}
	/**
	 * @Title: jsonToCollection
	 * @Description: TODO(将一个集合格式的json字符串转化为集合)
	 * @author mb.wang  
	 * @date 2013-10-18 下午4:56:12
	 * @param json
	 * @param collectionClass
	 * @param elementClasses
	 * @return T  返回类型
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException 
	 */
	public static <T> T jsonToCollection(String json,Class<?> collectionClass,Class<?> ...elementClasses) throws JsonParseException, JsonMappingException, IOException {
		final ObjectMapper om = new ObjectMapper();
		om.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
		JavaType javaType = om.getTypeFactory().constructParametricType(collectionClass, elementClasses);

		return om.readValue(json, javaType);
	}
	
	/**
	 * @Title: jsonToObject
	 * @Description: TODO(将json格式的字符串转化为map/list)
	 * @author mb.wang
	 * @date 2013-10-17 下午3:11:25
	 * @param params
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 *             Object 返回类型
	 */
	public static Object jsonToObject(String params) throws JsonParseException,
			JsonMappingException, IOException {
		if (params.length() <= 10) {
			return null;
		}

		Object obj = null;

		final ObjectMapper om = new ObjectMapper();
		om.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);

		String tm = params.substring(0, 1);
		if (!StringUtils.equals(tm, "{")) {// [{}]
			tm = params.substring(0, 2);
			if (StringUtils.equals(tm, "[{")) {
				obj = om.readValue(params, List.class);
			}
		} else {// {}
			obj = om.readValue(params, Map.class);
		}

		return obj;
	}
}
